import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
} from 'react-native';
import DataUtil from '../../util/DataUtil'
import LoginPage from "./loginAndRegister";
import MyToolbarSimple from "../../comps/normal/myToolBarSimple";
import ChooseList from "../../comps/list/myChooseProvinceList";
import NetUtil from "../../util/NetWorkUtil";
import UrlUtils from "../../util/UrlCollection";


let url = 'https://edu.bjzjmy.com:4455/register/city/schools?';
let city = 'city=';
let county = "&county=";
let province = "&province=";

const ITEM_HEIGHT = 40; //item的高度
const HEADER_HEIGHT = 30;  //分组头部的高度
const SEPARATOR_HEIGHT = 0; //分割线的高度

export default class ChooseProvince extends React.Component {
    constructor() {
        super();
        this.state = {
            loading: 'false',
            firstAll: [],
            data: '',
        };
    }

    render() {
        if (this.state.loading === 'false') {
            this.getData();
            return (
                <View style={{flex: 1}}>
                    <MyToolbarSimple th={this} title="选择学校"/>
                    <View style={styles.container}>
                        <Text>loading............</Text>
                    </View>
                </View>
            )
        } else {
            return (
                <View style={{flex: 1}}>
                    <MyToolbarSimple th={this} title="选择学校"/>
                    <ChooseList
                        comp={LoginPage}
                        data={this.state.data}
                        isSchool={true}
                        nav={this.props.navigator}
                    />
                </View>
            );
        }
    }

    /**
     * 请求数据
     */
    getData() {
        console.log("provinceId:" + this.props.provinceId);
        console.log("cityId:" + this.props.cityId);
        console.log("countyId:" + this.props.countyId);

        NetUtil.get(UrlUtils.chooseSchool,
            {
                'city': this.props.cityId,
                'province': this.props.provinceId,
                'county': this.props.countyId
            },
            (responseData) => {
                this.setState({
                    loading: 'true',
                    data: responseData
                });
            }
        )
    }

    /**
     * 吧json数据转化为sections数据
     * @param responseData
     *
     [
     {title: 'D', data: [{name:'Devin',id=0}]},
     {title: 'J', data: [{name:'Jackson',id=0},{name:'James',id=0}] },
     ]
     *
     */
    setData(responseData) {
        let jsonData = responseData.list;
        let data = '[';
        let firstAll = [];
        let item_end = '],';
        // 遍历字母
        // console.log(JSON.stringify(responseData));
        // console.log(responseData);

        for (let i = 0; i < jsonData.length; i++) {
            let first = jsonData[i].first;
            if (!DataUtil.contains(firstAll, first)) {
                //第一次出现的字母, 添加到集合中并生成新的item项
                firstAll[firstAll.length] = first;
            }
        }
        firstAll.sort();

        //字母数组已排序 , 开始按照字母顺序拼接
        for (let j = 0; j < firstAll.length; j++) {
            //预定义title
            let item = '{"title":"' + firstAll[j] + '","data":[';
            for (let z = 0; z < jsonData.length; z++) {
                if (firstAll[j] == jsonData[z].first) {
                    item = item + '{"name":"' + jsonData[z].name + '","id":"+jsonData[z].id+"},';
                }
            }
            item = item.substr(0, item.length - 1);
            item += '],';
            item += '"key":' + j + '},';
            // console.log(item);
            data += item
        }
        data += ']';
        return data;
    }

    /**
     * 吧json数据转化为sections的对象数据
     * @param responseData
     *
     [
     {title: 'D', data: [{name:'Devin',id=0}]},
     {title: 'J', data: [{name:'Jackson',id=0},{name:'James',id=0}] },
     ]
     *
     */
    _solveData(responseData) {
        console.log(JSON.stringify(responseData));
        let jsonData = responseData.list; //JSON数据
        let finalData = []; //最终数组对象
        let firstAll = []; //首字母集合

        //首先封装出现过的首字母
        for (let i = 0; i < jsonData.length; i++) {
            let first = (jsonData[i].simpleName)[0];
            if (!DataUtil.contains(firstAll, first)) {
                //第一次出现的字母, 添加到集合中并生成新的item项
                firstAll[firstAll.length] = first;
            }
        }
        firstAll.sort();

        //根据首字母进行对象的封装
        for (let j = 0; j < firstAll.length; j++) {
            let item = {}; //定义单个条目
            item.title = firstAll[j]; //条目的标题

            let item_obj = [];//条目中data的数组
            for (let z = 0; z < jsonData.length; z++) {
                if (firstAll[j] == (jsonData[z].simpleName)[0]) {
                    let inner = {};
                    inner.name = jsonData[z].name;
                    inner.key = jsonData[z].id;
                    item_obj[item_obj.length] = inner;
                }
            }
            item.data = item_obj; //条目数据
            item.keys = j;  //条目key
            finalData[j] = item
        }
        console.log(finalData);
        return finalData;
    }

    _onSectionselect = (section, index) => {
        //跳转到某一项
        // this.refs.list.scrollToIndex({animated: true, index: this.state.sectionSize[index]})
        this.refs.list.scrollToLocation({
            animated: true,
            sectionIndex: index - 1,
            itemIndex: 0,
            viewPosition: 0,
            viewOffset: 0
        })
    };

    getFirstAll(data) {
        //首先封装出现过的首字母
        let firstAll = [];
        let jsonData = data.list;
        for (let i = 0; i < jsonData.length; i++) {
            let first = (jsonData[i].simpleName)[0];
            if (!DataUtil.contains(firstAll, first)) {
                //第一次出现的字母, 添加到集合中并生成新的item项
                firstAll[firstAll.length] = first;
            }
        }
        firstAll.sort();
        return firstAll;
    }

    _getItemLayout(data, index) {
        let [length, separator, header] = [ITEM_HEIGHT, SEPARATOR_HEIGHT, HEADER_HEIGHT];
        return {length, offset: (length + separator) * index + header, index};
    };
}

class resource extends Component {
    render() {
        return (
            <View>
                <Text>资源</Text>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    sectionHeader: {
        paddingTop: 2,
        paddingLeft: 10,
        paddingRight: 10,
        paddingBottom: 2,
        fontSize: 14,
        fontWeight: 'bold',
        backgroundColor: 'rgba(247,247,247,1.0)',
    },
    item: {
        padding: 10,
        fontSize: 18,
        height: 44,
    },
    headerView: {
        justifyContent: 'center',
        height: HEADER_HEIGHT,
        paddingLeft: 20,
        backgroundColor: '#eee'
    },
    headerText: {
        fontSize: 15,
        // fontWeight: 'bold',
        // color: '#3cb775'
    },
    itemView: {
        justifyContent: 'center',
        backgroundColor: "#fff",
        flexDirection: 'column',
        padding: 12,
        alignItems: 'flex-start',
        height: ITEM_HEIGHT
    },
    navGroup: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#fff',
        height: 50,
        paddingHorizontal: 5
    },
    scenes: {
        flex: 1
    },
});
